package com._520it.wms.service.impl;

import com._520it.wms.domain.Department;
import com._520it.wms.domain.Employee;
import com._520it.wms.mapper.EmployeeMapper;
import com._520it.wms.query.PageResult;
import com._520it.wms.query.QueryObject;
import com._520it.wms.service.IEmployeeService;
import com._520it.wms.util.MD5;
import com._520it.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.security.Permission;
import java.util.List;

/**
 * Created by HASEE on 2017/11/14.
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void saveOrUpdate(Employee employee, Long[] roleIds) {
        if (employee.getId() != null) {
            //修改
            //删除原来的关联关系
            employeeMapper.deleteRelationWithRole(employee.getId());
            employeeMapper.updateByPrimaryKey(employee);
            if (roleIds != null) {
                for (Long roleId : roleIds) {
                    employeeMapper.insertRelactionWithRole(employee.getId(), roleId);
                }
            }
        } else {
            //新增
            //用md5对密码进行加密
            employee.setPassword(MD5.encode(employee.getPassword()));
            employeeMapper.insert(employee);
            //保存员工和角色的关系
            for (Long roleId : roleIds) {
                employeeMapper.insertRelactionWithRole(employee.getId(), roleId);

            }
        }

    }

    @Override
    public void delete(Long id) {
        //先删除关联关系
        employeeMapper.deleteRelationWithRole(id);
        //删除自己
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Employee get(Long id) {

        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int totalCount = employeeMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getCurrentPage(), qo.getPageSize());
        }
        //查询结果集
        List<Employee> list = employeeMapper.queryForList(qo);
        return new PageResult(qo.getCurrentPage(), qo.getPageSize(), list, totalCount);
    }

    @Override
    public void checkLogin(String username, String password) {
        Employee employee = employeeMapper.queryByUsernameAndpassword(username, MD5.encode(password));
        if (employee == null) {
            throw new RuntimeException("用户名或者密码错误");
        }
        //把用户信息存储到session中
        UserContext.setCurrentUser(employee);
        //查询权限
        List<String> permissionList = employeeMapper.queryPermissionListByEmpId(employee.getId());


    }

    @Override
    public void batchDelete(Long[] ids) {

        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee queryByName(String username) {


        return employeeMapper.queryByName(username);
    }

}

